// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html


import AiScript from "./ai-script";

const {ccclass, property, executionOrder} = cc._decorator;

@ccclass
@executionOrder(11)
export default class Camera extends cc.Component {

  @property(cc.Node)
  private _focus: cc.Node = null

  onLoad() {
    if (this._focus) {
      this._focus.on('kill', this.onKill, this)
    }
  }

  get focus(): cc.Node {
    return this._focus;
  }

  set focus(value: cc.Node) {
    if (this._focus) {
      this._focus.targetOff(this)
    }
    this._focus = value;
    this._focus.on('kill', this.onKill, this)
  }

  onKill(e) {
  }

  acc = 0;

  update(dt) {
    this.acc += dt
    if (this.acc > 1) {
      const ai = this._focus.getComponent(AiScript)
      if (ai) {
      }
      this.acc = 0;
    }
    this.syncGameBoardPos();
  }

  private syncGameBoardPos() {
    this.node.x = -this._focus.x
    this.node.y = -this._focus.y
  }
}
